<?php

namespace App\Providers;

use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Tymon\JWTAuth\JWTGuard;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        Auth::extend('jwt', function ($app, $name, array $config) {

            return new JWTGuard(Auth::createUserProvider($config['provider']));
        });
    }

    /**
     * Boot the authentication services for the application.
     *
     * @return void
     */
    public function boot()
    {
        // Here you may define how you wish users to be authenticated for your Lumen
        // application. The callback which receives the incoming request instance
        // should return either a User instance or null. You're free to obtain
        // the User instance via an API token or any other method necessary.

//        $this->app['auth']->viaRequest('api', function ($request) {
//            if ($request->input('api_token')) {
//                return User::where('api_token', $request->input('api_token'))->first();
//            }
//        });

        $this->app['auth']->provider('custom_eloquent_agent_driver', function ($app, $config) {
            return new CustomEloquentAgentProvider($this->app['hash'], $config['model']);
        });

//        // 超级管理员
//        Gate::before(function ($user, $ability) {
//            return Auth::user()->isSuperAdmin() ? true : null;
//        });
    }
}
